home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / RCSC.ZIP / DEMO86 / ASYN8250.C next >
C/C++ Source or Header  |  1997-01-12  |  3KB  |  134 lines

  1. #include <stdio.h>
  2.  
  3. /* 8259 interrupt controller interrupt mask register port# */
  4. #define MASK8259    0x21
  5.  
  6. /* async 8250 constants */
  7. /* these parameters are for com2 */
  8. #define IOBASE        0x2F0
  9. #define INTVEC        0x0B
  10. #define DEVMASKBIT    0x08
  11.  
  12. /* 8250 control register port number */
  13. #define RLINECTR    (IOBASE + 0x0B)
  14. #define RLBAUD        (IOBASE + 0x08)
  15. #define RHBAUD        (IOBASE + 0x09)
  16. #define RLINESTAT    (IOBASE + 0x0D)
  17. #define RRECV        (IOBASE + 0x08)
  18. #define RXMIT        (IOBASE + 0x08)
  19. #define RINTEN        (IOBASE + 0x09)
  20. #define RMODEMC        (IOBASE + 0x0C)
  21. #define RINTID        (IOBASE + 0x0A)
  22. #define RMODEMSTAT    (IOBASE + 0x0E)
  23.  
  24. #define TXREADY        0x20    /* ready to transmit */
  25. #define SETDIVISOR    0x80    /* set divisor */
  26. #define L2400        0x30    /* low value for clock divisor */
  27. #define H2400        0x00    /* high value for clock divisor */
  28. #define DATA8        0x03    /* 8-bit data */
  29. #define DCD        0x80    /* data carrier detect */
  30. #define DSR        0x20    /* data set ready */
  31. #define CTS        0x10    /* clear to send */
  32. #define DTR        0x01    /* data terminal ready */
  33. #define RTS        0x02    /* request to send */
  34. #define OUT2        0x08    /* output 2 signal */
  35. #define TXEMPTY        0x02    /* Tx holding register empty interrupt */
  36.  
  37. #define COMPLETED    0xFF    /* user-defined completion return code */
  38.  
  39. static int pos;        /* position of char in msg to send */
  40. static char msg[128];    /* message to send */
  41. static int linefeed;    /* time to send a linefeed */
  42.  
  43. /* send a char over the asyn link */
  44. sendchar(ch)
  45. char ch;
  46. {
  47.     /* check for modem status */
  48.     if (inpbyte(RMODEMSTAT) & (DCD | DSR | CTS)) {
  49.         /* wait till Tx is ready and send */
  50.         while ((inpbyte(RLINESTAT) & TXREADY) != TXREADY);
  51.         outpbyte(RXMIT, ch);
  52.         return 0;
  53.         }
  54.  
  55.     return 1;    
  56. }
  57.  
  58. /* initialize the 8250 chip */
  59. void Init8250()
  60. {
  61.     /* set baud rate at 2400, no parity, 8-bit data */
  62.     outpbyte(RLINECTR, SETDIVISOR);
  63.     outpbyte(RLBAUD, L2400);
  64.     outpbyte(RHBAUD, H2400);
  65.     outpbyte(RLINECTR, DATA8);
  66.  
  67.     /* set modem control */
  68.     outpbyte(RMODEMC, (DTR | RTS | OUT2));
  69.  
  70.     /* enable 8250 Tx holding register empty interrupt */
  71.     outpbyte(RINTEN, TXEMPTY);
  72.  
  73.     /* enable 8259 interrupt controller for com2 */
  74.     outpbyte(MASK8259, inpbyte(MASK8259) & ~DEVMASKBIT);
  75. }
  76.  
  77. /* interrupt service routine to send a message. Note: a non-zero
  78.     return value wakes up the xmitter task which initiated
  79.     the transmission by calling StartIO */
  80. interrupt asynsend(INTVEC)
  81. {
  82.     char ch;
  83.     int retcode;
  84.  
  85.     if (linefeed) {
  86.         /* time to send a linefeed */
  87.         linefeed = 0;
  88.         retcode = sendchar(LF);
  89.         }
  90.     else if ((ch = msg[++pos]) != 0) {
  91.         /* translate LF to CR followed by LF */
  92.         if (ch == LF)
  93.             {
  94.             retcode = sendchar(CR);
  95.             linefeed = 1;
  96.             }
  97.         else
  98.             /* send char as is */
  99.             retcode = sendchar(ch);
  100.         }
  101.     else
  102.         return COMPLETED;
  103.  
  104.     return retcode;
  105. }
  106.  
  107. /* start interrupt-driven message transmission */
  108. startx()
  109. {
  110.     pos = 0;
  111.     return sendchar(msg[0]);
  112. }
  113.  
  114. /* xmitter task declaration: note use of high task priority */
  115. void task xmitter(256)
  116. {
  117.     int status;
  118.  
  119.     Init8250();    /* init 8250 */
  120.  
  121.     while (1) {
  122.         /* get message and print it */
  123.         GetMsg(msg);
  124.         printf(msg);
  125.  
  126.         /* start i/o operation to send message to remote console. 
  127.             Only return when operation is completed or failed */
  128.         StartIO(INTVEC, startx, &status);
  129.         if (status != COMPLETED)
  130.             printf("Async transmit error: %d\n", status);
  131.         }    
  132. }
  133.  
  134.